<template>
  <div class="app-container">
    <SearchFilter
      size="medium"
      :maxShow="3"
      @search="handleQuery"
      @reset="resetQuery"
    >
      <el-form
        :model="queryParams"
        label-position="right"
        label-width="70px"
        ref="queryForm"
      >
        <el-row :gutter="20">
          <el-col :span="7">
            <el-form-item label="工单编号" prop="equipmentId">
              <el-input
                v-model="queryParams.equipmentId"
                placeholder="请输入设备id"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="项目编号" prop="projectId">
              <el-input
                v-model="queryParams.projectId"
                placeholder="请输入项目id"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="设备名称" prop="areaId">
              <el-input
                v-model="queryParams.areaId"
                placeholder="请输入片区id"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="所属项目" prop="position">
              <el-input
                v-model="queryParams.position"
                placeholder="请输入定位"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="所属片区" prop="endDate">
              <el-date-picker
                clearable
                v-model="queryParams.endDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择截止时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="维修人员" prop="owner">
              <el-input
                v-model="queryParams.owner"
                placeholder="请输入业主负责人"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="设备类型" prop="ownerUnit">
              <el-input
                v-model="queryParams.ownerUnit"
                placeholder="请输入业主单位"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="设备厂商" prop="unitArea">
              <el-input
                v-model="queryParams.unitArea"
                placeholder="请输入单位地址"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="项目经理" prop="ownerPhone">
              <el-input
                v-model="queryParams.ownerPhone"
                placeholder="请输入业主电话"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="工单状态" prop="contractDate">
              <el-date-picker
                clearable
                v-model="queryParams.contractDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择合同时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="项目经理" prop="ownerPhone">
              <el-input
                v-model="queryParams.ownerPhone"
                placeholder="请输入业主电话"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </SearchFilter>

    <div class="content-container">
      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="1.5">
          <p class="header-title">报修工单列表</p>
        </el-col>
        <div>
          <el-col :span="1.5" v-if="ids.length">
            <el-button
              size="small"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['work:repair:remove']"
              >批量删除</el-button
            >
          </el-col>
          <el-col :span="1.5" v-if="ids.length">
            <el-button
              size="small"
              :disabled="multiple"
              icon="el-icon-download"
              @click="handleExport"
              v-hasPermi="['work:repair:export']"
              >批量导出</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
              v-hasPermi="['work:repair:add']"
              >新增报修</el-button
            >
          </el-col>
        </div>
      </el-row>

      <el-table
        v-loading="loading"
        :data="repairList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="主键" align="center" width="120" prop="id" /> -->
        <el-table-column
          label="设备"
          align="center"
          width="120"
          prop="equipmentName"
        />
        <el-table-column
          label="项目"
          align="center"
          width="120"
          prop="projectName"
        />
        <el-table-column
          label="片区"
          align="center"
          width="120"
          prop="areaName"
        />
        <el-table-column
          label="定位"
          align="center"
          width="120"
          prop="position"
        />
        <el-table-column
          label="截止时间"
          align="center"
          prop="endDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="业主负责人"
          align="center"
          width="120"
          prop="owner"
        />
        <el-table-column
          label="业主单位"
          align="center"
          width="120"
          prop="ownerUnit"
        />
        <el-table-column
          label="单位地址"
          align="center"
          width="120"
          prop="unitArea"
        />
        <el-table-column
          label="业主电话"
          align="center"
          width="120"
          prop="ownerPhone"
        />
        <el-table-column
          label="合同时间"
          align="center"
          prop="contractDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.contractDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="合同到期时间"
          align="center"
          prop="contractEndDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.contractEndDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="项目经理"
          align="center"
          width="120"
          prop="projectManager"
        />
        <el-table-column
          label="项目经理电话"
          align="center"
          width="120"
          prop="projectManagerPhone"
        />
        <el-table-column
          label="维修人员"
          align="center"
          width="120"
          prop="repairPerson"
        />
        <el-table-column
          label="维修人员电话"
          align="center"
          width="120"
          prop="repairPersonPhone"
        />
        <el-table-column
          label="备注"
          align="center"
          width="120"
          prop="remark"
        />
        <el-table-column
          label="报修视频"
          align="center"
          width="120"
          prop="repairVideo"
        />
        <el-table-column
          label="报修图片"
          align="center"
          width="120"
          prop="repairImg"
        />
        <el-table-column
          label="报修电话"
          align="center"
          width="120"
          prop="repairPhone"
        />
        <el-table-column
          label="报修备注"
          align="center"
          width="120"
          prop="repairRemark"
        />
        <el-table-column
          label="工单状态"
          align="center"
          width="120"
          prop="status"
        >
          <template slot-scope="scope">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <span class="wait-round" v-if="scope.row.status === 0"></span>
              <span class="already-round" v-if="scope.row.status === 1"></span>
              <span>{{ changeStatus(scope.row.status) }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="工单标签"
          align="center"
          width="120"
          prop="reStatus"
        >
          <template slot-scope="scope">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <span
                class="wait-round"
                v-if="
                  ['申请延期', '拒绝延期', '同意延期'].includes(
                    changeReStatus(scope.row.statusText)
                  )
                "
              ></span>
              <!-- <span class="time-out-round " v-if="scope.row.status1 === 1"></span> -->
              <span
                class="delay-round"
                v-if="
                  ['申请退单', '拒绝退单', '同意退单'].includes(
                    changeReStatus(scope.row.statusText)
                  )
                "
              ></span>
              <span>{{ changeReStatus(scope.row.statusText) }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="220"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              class="text-text table-btn"
              v-hasPermi="['work:repair:query']"
              @click="getRouterInfo(scope.row)"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              class="text-danger table-btn"
              @click="handleDelete(scope.row)"
              v-hasPermi="['work:repair:remove']"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-if="changeReStatus(scope.row.statusText) === '申请退单'"
              @click="getRouterInfo(scope.row)"
              >退单查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              class="text-warning"
              v-if="changeReStatus(scope.row.statusText) === '申请延期'"
              @click="getRouterInfo(scope.row)"
              >延期查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-if="changeReStatus(scope.row.statusText) === '申请转单'"
              @click="getRouterInfo(scope.row)"
              >转单查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改报修工单信息对话框 -->
    <el-dialog
      :title="title"
      custom-class="dialog"
      :visible.sync="open"
      append-to-body
      :close-on-click-modal="false"
      width="740px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-position="top"
        label-width="80px"
      >
        <!-- <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="所属项目" prop="projectId">
              <el-select v-model="form.projectId" placeholder="请选择所属项目">
                <el-option
                  v-for="item in projectOptions"
                  :key="item.id"
                  :label="item.projectName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="所属片区" prop="areaId">
              <el-select v-model="form.areaId" placeholder="请输入所属片区">
                <el-option
                  v-for="item in areaOptions"
                  :key="item.id"
                  :label="item.areaName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="设备" prop="equipmentId">
              <el-select
                clearable
                v-model="form.equipmentId"
                placeholder="请选择设备"
                @change="selectChange"
              >
                <el-option
                  v-for="item in deviceList"
                  :key="item.value"
                  :value="item.value"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="设备类型" prop="equipmentType">
              <el-input
                v-model="form.equipmentType"
                disabled
                placeholder="请输入项目id"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="设备编号" prop="equipmentCode">
              <el-input
                v-model="form.equipmentCode"
                disabled
                placeholder="请输入片区id"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="设备厂商" prop="equipmentFirm">
              <el-input
                v-model="form.equipmentFirm"
                disabled
                placeholder="请输入定位"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="所属项目" prop="projectId">
              <el-select
                v-model="form.projectId"
                disabled
                placeholder="请选择所属项目"
              >
                <el-option
                  v-for="item in projectOptions"
                  :key="item.id"
                  :label="item.projectName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="所属片区" prop="areaId">
              <el-select
                v-model="form.areaId"
                disabled
                placeholder="请输入所属片区"
              >
                <el-option
                  v-for="item in areaOptions"
                  :key="item.id"
                  :label="item.areaName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="定位" prop="position">
              <el-input
                v-model="form.position"
                disabled
                placeholder="请输入片区id"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="截止时间" prop="endDate">
              <el-date-picker
                clearable
                v-model="form.endDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择截止时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="业主单位" prop="ownerUnit">
              <el-input
                v-model="form.ownerUnit"
                disabled
                placeholder="请输入业主单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="单位地址" prop="unitArea">
              <el-input
                v-model="form.unitArea"
                disabled
                placeholder="请输入单位地址"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="业主负责人" prop="owner">
              <el-input
                v-model="form.owner"
                disabled
                placeholder="请输入业主负责人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="业主负责人电话" prop="ownerPhone">
              <el-input
                v-model="form.ownerPhone"
                disabled
                placeholder="请输入业主电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="合同时间" prop="contractDate">
              <el-date-picker
                clearable
                disabled
                v-model="form.contractDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择合同时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合同到期时间" prop="contractEndDate">
              <el-date-picker
                clearable
                disabled
                v-model="form.contractEndDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择合同到期时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="项目经理" prop="projectManager">
              <el-select
                v-model="form.projectManager"
                disabled
                placeholder="请输入项目经理"
                @change="managerChange"
              >
                <el-option
                  v-for="item in managerList"
                  :key="item.nickName"
                  :label="item.nickName"
                  :value="item.nickName"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="项目经理电话" prop="projectManagerPhone">
              <el-input
                v-model="form.projectManagerPhone"
                disabled
                placeholder="请输入项目经理电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="维修人员" prop="repairPerson">
              <el-select
                v-model="form.repairPerson"
                multiple
                placeholder="请输入维修人员"
                @change="repairChange"
              >
                <el-option
                  v-for="item in repairOptions"
                  :key="item.nickName"
                  :label="item.nickName"
                  :value="item.nickName"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="维修人员电话" prop="repairPersonPhone">
              <el-input
                v-model="form.repairPersonPhone"
                disabled
                placeholder="请输入维修人员电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="报修图片" prop="repairImg">
              <image-upload v-model="form.repairImg" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="报修视频" prop="repairVideo">
              <file-upload v-model="form.repairVideo" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="报修联系人" prop="repairUser">
              <el-input
                v-model="form.repairUser"
                placeholder="请输入保修电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="报修联系方式" prop="repairPhone">
              <el-input
                v-model="form.repairPhone"
                placeholder="请输入保修电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="报修备注" prop="repairRemark">
              <el-input
                v-model="form.repairRemark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepair,
  getRepair,
  delRepair,
  addRepair,
  updateRepair,
} from "@/api/work/repair";
import { listArea } from "@/api/resource/area";
import { listProject } from "@/api/resource/project";
import { getUserList } from "@/api/menu";
import { listEquipment, getEquipment } from "@/api/resource/equipment";
import SearchFilter from "@/components/SearchFile";

export default {
  name: "Repair",
  components: { SearchFilter },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修工单信息表格数据
      repairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        projectId: null,
        areaId: null,
        position: null,
        endDate: null,
        owner: null,
        ownerUnit: null,
        unitArea: null,
        ownerPhone: null,
        contractDate: null,
        contractEndDate: null,
        projectManager: null,
        projectManagerPhone: null,
        repairPerson: null,
        repairPersonPhone: null,
        repairVideo: null,
        repairImg: null,
        repairUser: null,
        repairPhone: null,
        repairRemark: null,
        status: null,
        reStatus: null,
        version: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipmentId: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        endDate: [
          { required: true, message: "截止时间不能为空", trigger: "blur" },
        ],
        repairPerson: [
          { required: true, message: "维修人员不能为空", trigger: "blur" },
        ],
        repairUser: [
          { required: true, message: "报修联系人不能为空", trigger: "blur" },
        ],
        repairPhone: [
          {
            required: true,
            message: "报修人联系方式不能为空",
            trigger: "blur",
          },
          {
            pattern: /(1[34578]\d{9})$/,
            message: "请填写正确的手机号码",
            trigger: "change",
          },
        ],
      },
      projectOptions: [],
      areaOptions: [],
      deviceList: [],
      managerList: [],
      repairOptions: [],
    };
  },
  created() {
    this.getProjectList();
    this.getAreaList();
    this.getUserListHandle();
    this.getDeviceList();
    this.getList();
  },
  methods: {
    /** 查询项目 */
    async getProjectList() {
      const res = await listProject({
        deptId: this.$store.state.user.user.deptId,
        pageNum: -1,
      });
      this.projectOptions = res.rows;
    },
    /** 查询片区 */
    async getAreaList() {
      const res = await listArea({
        deptId: this.$store.state.user.user.deptId,
        pageNum: -1,
      });
      this.areaOptions = res.rows;
    },
    async getUserListHandle() {
      const deptCode = this.$store.state.user.user.dept.deptCode;
      const roleKey = deptCode.substring(deptCode.length - 4); // todo
      const managerRes = await getUserList({ roleKey: roleKey + "-manager" })
      if (managerRes.data.length) {
        this.managerList = managerRes.data.map((item) => {
          return { ...item, userId: item.userId + "" };
        });
      }
      const repairRes = await getUserList({ roleKey: roleKey + "-repair" });
      if (repairRes.data.length) {
        this.repairOptions = repairRes.data.map((item) => {
          return { ...item, userId: item.userId + "" };
        });
      }
    },

    managerChange(e) {
      let obj = this.managerList.find((item) => item.nickName === e);
      this.form.projectManagerPhone = obj.phonenumber;
    },
    repairChange(e) {
      this.form.repairPersonPhone = "";
      let phoneStr = [];
      e.forEach((items) => {
        let obj = this.repairOptions.find((item) => item.nickName === items);
        phoneStr.push(obj.phonenumber);
      });
      this.form.repairPersonPhone = phoneStr.toString();
    },
    /** 设备列表 */
    async getDeviceList() {
      const res = await listEquipment({
        deptId: this.$store.state.user.user.deptId,
        pageNum: -1,
      });
      if (res.rows.length) {
        this.deviceList = res.rows.map((item) => {
          return { name: item.equipmentName, value: item.id };
        });
      } else {
        this.deviceList = [];
      }
    },
    /** 查询设备详情 */
    async selectChange(val) {
      const res = await getEquipment(val);
      console.log(res);
      this.form = {
        equipmentId: res.data.id,
        equipmentType: res.data.equipmentType + "",
        equipmentCode: res.data.equipmentCode,
        equipmentFirm: res.data.equipmentFirm,
        projectId: res.data.projectId,
        areaId: res.data.areaId,
        position: res.data.position,
        endDate: res.data.endDate,
        ownerUnit: res.data.ownerUnit,
        unitArea: res.data.unitArea,
        owner: res.data.owner,
        ownerPhone: res.data.ownerPhone,
        contractDate: res.data.contractDate,
        contractEndDate: res.data.contractEndDate,
        projectManager: res.data.projectManager,
        projectManagerPhone: res.data.projectManagerPhone,
        // repairPerson: res.data.repairPerson,
        // repairPersonPhone: res.data.repairPersonPhone,
        remark: res.data.remark,
        repairImg: res.data.repairImg,
        repairVideo: res.data.repairVideo,
      };
    },
    /** 查询报修工单信息列表 */
    getList() {
      this.loading = true;
      listRepair({
        ...this.queryParams,
        deptId: this.$store.state.user.user.deptId,
        orderByColumn: "id",
        isAsc: "desc",
      }).then((response) => {
        this.repairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    changeStatus(val) {
      let str = "";
      switch (val) {
        case 0:
          str = "待处理";
          break;
        case 1:
          str = "已处理";
          break;
        default:
          break;
      }
      return str;
    },
    changeReStatus(val) {
      if (!(val && val.length)) return;
      let arr = JSON.parse(val);
      let obj = arr[arr.length - 1];
      if (obj.statusType === 2) {
        if (obj.status === 0) {
          return "申请退单";
        }
        if (obj.status === 1) {
          return "拒绝退单";
        }
        if (obj.status === 2) {
          return "同意退单";
        }
      }
      if (obj.statusType === 3) {
        if (obj.status === 0) {
          return "申请延期";
        }
        if (obj.status === 1) {
          return "拒绝延期";
        }
        if (obj.status === 2) {
          return "同意延期";
        }
      }
      if (obj.statusType === 4) {
        if (obj.status === 0) {
          return "申请转单";
        }
        if (obj.status === 1) {
          return "拒绝转单";
        }
        if (obj.status === 2) {
          return "同意转单";
        }
      }
    },
    /** 跳转详情 */
    getRouterInfo({ id }) {
      this.$router.push("/work/repairQuery/" + id);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentId: null,
        projectId: null,
        areaId: null,
        position: null,
        endDate: null,
        owner: null,
        ownerUnit: null,
        unitArea: null,
        ownerPhone: null,
        contractDate: null,
        contractEndDate: null,
        projectManager: null,
        projectManagerPhone: null,
        repairPerson: [],
        repairPersonPhone: null,
        remark: null,
        repairVideo: null,
        repairImg: null,
        repairPhone: null,
        repairRemark: null,
        status: null,
        reStatus: null,
        version: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报修工单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRepair(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报修工单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let str = "";
          console.log(this.form);
          if (this.form.repairPerson.length) {
            str = this.form.repairPerson.toString();
          }
          if (this.form.id != null) {
            updateRepair({
              ...this.form,
              repairPerson: str,
              deptId: this.$store.state.user.user.deptId,
            }).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepair({
              ...this.form,
              repairPerson: str,
              deptId: this.$store.state.user.user.deptId,
            }).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除报修工单信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRepair(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "work/repair/export",
        {
          ...this.queryParams,
        },
        `repair_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
